//
//  Qb_JoinViewController.m
//  Cd_Qb
//
//  Created by mac on 2018/3/9.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "Qb_JoinViewController.h"

#import "Qb_JoinTableViewCell.h"

#import "MJRefresh.h"
@interface Qb_JoinViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *JointableView;
@property (nonatomic,strong) NSMutableArray *JoinDate;
@end

@implementation Qb_JoinViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"会员等级";
     [self.view addSubview:self.JointableView];
}


#pragma mark -- Action
-(void)loadNewData{
    //这里假设2秒之后获取到了最新的数据，刷新tableview，并且结束刷新控件的刷新状态
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //刷新列表
        [weakSelf.JointableView reloadData];
        //拿到当前的刷新控件，结束刷新状态
        [weakSelf.JointableView.mj_header endRefreshing];
    });
}

-(void)loadMoreData{
    //这里假设2秒之后获取到了更多的数据，刷新tableview，并且结束刷新控件的刷新状态
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //刷新列表
        [weakSelf.JointableView reloadData];
        //拿到当前的刷新控件，结束刷新状态
        [weakSelf.JointableView.mj_footer endRefreshing];
    });
}

#pragma mark --UITableViewDelegate&&UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.JoinDate.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Qb_JoinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell loadData:self.JoinDate[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 175*AdaptiveScale_W;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld ----",indexPath.row);
}

-(UITableView *)JointableView{
    if (!_JointableView) {
        _JointableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        _JointableView.delegate = self;
        _JointableView.dataSource = self;
        _JointableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _JointableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _JointableView.showsVerticalScrollIndicator = NO;
        _JointableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_JointableView registerNib:[UINib nibWithNibName:@"Qb_JoinTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        WS(weakself);
        _JointableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself loadNewData];
        } ];
        
        _JointableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            //刷新时候，需要执行的代码。一般是请求更多数据，请求成功之后，刷新列表
            [weakself loadMoreData];
        }];
    }
    return _JointableView;
}

-(NSMutableArray *)JoinDate{
    if (!_JoinDate) {
        _JoinDate = [NSMutableArray array];
        NSArray *CellBackAry = @[@"Qb_JoinCellBack01",@"Qb_JoinCellBack02",@"Qb_JoinCellBack03",@"Qb_JoinCellBack04"];
        NSArray *LvNameBackAry = @[@"Qb_JoinCellimg01",@"Qb_JoinCellimg02",@"Qb_JoinCellimg03",@"Qb_JoinCellimg04"];
        NSArray *LvImgAry = @[@"Qb_JoinCellLv01",@"Qb_JoinCellLv02",@"Qb_JoinCellLv03",@"Qb_JoinCellLv04"];
       
        for (int i = 0; i<CellBackAry.count; i++) {
             NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:CellBackAry[i] forKey:@"cellback"];
             [dic setObject:LvNameBackAry[i] forKey:@"lvnameback"];
             [dic setObject:LvImgAry[i] forKey:@"lvimg"];
            [_JoinDate addObject:dic];
        }
        
    }
    return _JoinDate;
}


@end
